package com.mornd.gulimall.search.entity;

import com.mornd.gulimall.common.core.utils.BigDecimals;
import com.mornd.gulimall.search.constant.EsConstant;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author: mornd
 * @dateTime: 2023/8/13 - 19:46
 * 搜索页面请求参数实体类
 */

public class SearchParam {
    /**
     * 搜索关键字
     */
    private String keyword;

    /**
     * 3级分类id
     */
    private Long catalog3Id;

    /**
     * 排序条件
     * saleCount_asc/desc 销量
     * skuPrice_asc/desc 价格
     * hotScore_asc/desc 热度
     */
    private String sort;

    // 过滤条件

    /**
     * 是否只显示有货
     * 0：无库存，1：有库存
     */
    private Integer hasStock;

    /**
     * 价格区间
     */
    // >=
    private BigDecimal priceL;
    // <=
    private BigDecimal priceR;

    /**
     * 品牌id(多选)
     */
    private List<Long> brandId;

    /**
     * 属性筛选条件
     * 属性id_属性值1:属性值2
     * attrs=1_5存:6寸&attrs=2_16GB:32GB
     */
    private List<String> attrs;

    /**
     * 页码
     */
    private Long pageNum;

    // getter & setter

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public Long getCatalog3Id() {
        return catalog3Id;
    }

    public void setCatalog3Id(Long catalog3Id) {
        this.catalog3Id = catalog3Id;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public Integer getHasStock() {
        return hasStock;
    }

    public void setHasStock(Integer hasStock) {
        this.hasStock = hasStock;
    }

    public BigDecimal getPriceL() {
        return priceL;
    }

    public void setPriceL(BigDecimal priceL) {
        if(BigDecimals.isValid(priceL)) {
            this.priceL = BigDecimals.setScale(priceL);
        } else {
            this.priceL = null;
        }
    }

    public BigDecimal getPriceR() {
        return priceR;
    }

    public void setPriceR(BigDecimal priceR) {
        if(BigDecimals.isValid(priceR)) {
            this.priceR = BigDecimals.setScale(priceR);
        } else {
            this.priceR = null;
        }
    }

    public List<Long> getBrandId() {
        return brandId;
    }

    public void setBrandId(List<Long> brandId) {
        this.brandId = brandId;
    }

    public List<String> getAttrs() {
        return attrs;
    }

    public void setAttrs(List<String> attrs) {
        this.attrs = attrs;
    }

    public Long getPageNum() {
        return (pageNum == null || pageNum < 1L) ? 1L : pageNum;
    }

    public void setPageNum(Long pageNum) {
        this.pageNum = pageNum;
    }

    /**
     * 封装 es 需要的 from 参数
     *
     * @return
     */
    public Long getFrom() {
        return (getPageNum() - 1) * EsConstant.PRODUCT_PAGE_SIZE;
    }
}
